/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.earthquake.buildingdesign;

import gov.fema.hazus.bdc.shared.entity.EarthquakeBuildingDesign;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EarthquakeBuildingDesignTextBoxKeyListener implements EarthquakeBuildingDesignTextBoxKeyListenerInterface {

	private EarthquakeBuildingDesign earthquakeBuildingDesign;
	
	public EarthquakeBuildingDesignTextBoxKeyListener(EarthquakeBuildingDesign earthquakeBuildingDesign)
	{
		this.earthquakeBuildingDesign = earthquakeBuildingDesign;
	}
	
	public void onFocus(Widget arg0) {
		// do nothing
	}

	public void onLostFocus(Widget arg0) {

		TextBox textBox = (TextBox) arg0;
		String boxName = textBox.getName();
		
		// get the form data as string, long or int
		String value = textBox.getText();
		Double doubleValue = null;
		Integer intValue = null;
		try {
			intValue = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			// swallow
		}

		try {
			doubleValue = Double.parseDouble(value);
		} catch (NumberFormatException e) {
			// swallow
		}
		
		GWT.log("text box name=" + boxName, null);
		
		if( boxName.equals( EarthquakeBuildingDesign.DESIGNYEAR ) )
		{
			GWT.log( EarthquakeBuildingDesign.DESIGNYEAR + ": " + value, null);
			this.earthquakeBuildingDesign.setDesignYear(intValue);
		} else if ( boxName.equals( EarthquakeBuildingDesign.DESIGNBASESHEAR ) ) {
			GWT.log( EarthquakeBuildingDesign.DESIGNBASESHEAR + ": " + value, null);
			this.earthquakeBuildingDesign.setDesignBaseShear(doubleValue);
		} else if ( boxName.equals( EarthquakeBuildingDesign.PERCENTAGECONTENTSONGROUND ) ) {
			GWT.log( EarthquakeBuildingDesign.PERCENTAGECONTENTSONGROUND + ": " + value, null);
			this.earthquakeBuildingDesign.setPercentageContentsOnGround(intValue);		
		} else {
			// do nothing
		}	
		
	}

	public void updateEarthquakeBuildingDesign(EarthquakeBuildingDesign earthquakeBuildingDesign) {
		this.earthquakeBuildingDesign = earthquakeBuildingDesign;
	}

}
